package com.nnxy.worktostudy.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nnxy.worktostudy.system.dto.request.FunRequestDto;
import com.nnxy.worktostudy.system.entity.Apply;
import com.nnxy.worktostudy.system.entity.Bill;
import com.nnxy.worktostudy.system.service.FundService;
import com.nnxy.worktostudy.utils.ResponseFormat;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author : lu'qz
 * @create 2021/10/15 17:59
 */
@RestController
@RequestMapping("/system/fund")
public class FundController {

    @Resource
    private FundService fundService;

    /**
     * 获取剩余资金，对接其他系统接口
     * @return ResponseFormat
     */
    @RequestMapping("/getMoney")
    public ResponseFormat getMoney() {
        Map<String,Object> result = new HashMap<>(1);
        result.put("money",90000);
        return ResponseFormat.succeed().setData(result);
    }

    /**
     * 项目岗位结构关系的 树
     * @return ResponseFormat
     */
    @RequestMapping("/jobTree")
    public ResponseFormat jobTree() {
        Map<String,Object> result = new HashMap<>(1);
        result.put("jobTree", fundService.getJobTree());
        return ResponseFormat.succeed().setData(result);
    }

    /**
     * 获取出账单 分页
     * @param obj obj
     * @return ResponseFormat
     */
    @RequestMapping("/getBillList")
    public ResponseFormat getBillList(@RequestBody FunRequestDto obj) {
        QueryWrapper<Bill> queryWrapper = new QueryWrapper<>();
        Map<String,Object> result = fundService.page(queryWrapper, obj.getPageSize(), obj.getCurrentPage());
        return ResponseFormat.succeed().setData(result);
    }

    /**
     * 搜索 分页
     * @param obj obj
     * @return ResponseFormat
     */
    @RequestMapping("/select")
    public ResponseFormat select(@RequestBody FunRequestDto obj) {
        QueryWrapper<Bill> queryWrapper = new QueryWrapper<>();

        if (obj.getId() != null) {
            queryWrapper.eq("id", obj.getId());
        } else if (obj.getStartDate() != null || obj.getEndDate() != null) {
            queryWrapper.between("create_time", obj.getStartDate(), obj.getEndDate());
        }

        Map<String, Object> result = fundService.page(queryWrapper, obj.getPageSize(), obj.getCurrentPage());

        return ResponseFormat.succeed().setData(result);
    }

    /**
     * 获取项目列表 分页
     * @param obj obj
     * @return ResponseFormat
     */
    @RequestMapping("/getProjectList")
    public ResponseFormat getProjectList(@RequestBody FunRequestDto obj) {
        Map<String, Object> result = fundService.getProjectList(obj.getPageSize(), obj.getCurrentPage());

        return ResponseFormat.succeed().setData(result);
    }

    @RequestMapping("/audit")
    public ResponseFormat v(@RequestBody FunRequestDto obj) {
        if (obj.getStatus() != 1 && obj.getStatus() != 2 ) {
            return ResponseFormat.failed().setMessage("状态值超出范围！");
        }

        if (fundService.auditProject(obj.getId(), obj.getStatus())) {
            return ResponseFormat.succeed().setMessage("审核成功！");
        }

        return ResponseFormat.failed().setMessage("审核失败！");
    }

}
